﻿using Lifebucket.Data.Repositories;
using Lifebucket.Data.Infrastructure;
using Lifebucket.Domain.Commands;
using Lifebucket.CommandProcessor.Command;
using Lifebucket.Model;

namespace Lifebucket.Domain.Handlers
{
    public class CreateOrUpdateBucketItemHandler : ICommandHandler<CreateOrUpdateBucketItemCommand>
    {
        private readonly IBucketItemRepository bucketItemRepository;
        private readonly IUnitOfWork unitOfWork;
        public CreateOrUpdateBucketItemHandler(IBucketItemRepository bucketItemRepository, IUnitOfWork unitOfWork)
        {
            this.bucketItemRepository = bucketItemRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(CreateOrUpdateBucketItemCommand command)
        {
            var bucketItem = new BucketItem
            {
                BucketItemId = command.BucketItemId,
                Label = command.Label,
                Description = command.Description
            };
            if (bucketItem.BucketItemId == 0)
                bucketItemRepository.Add(bucketItem);
            else
                bucketItemRepository.Update(bucketItem);
            unitOfWork.Commit();
            return new CommandResult(true);
        }
    }
}
